﻿using HandyControl.Controls;
using HardToolApp.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HardToolApp.Model
{
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum uart_parity
    {
        [Description("无校验")]
        UART_PARITY_NONE = 0,
        [Description("奇校验")]
        UART_PARITY_ODD,
        [Description("偶校验")]
        UART_PARITY_EVEN,
    }
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum uart_stop_bits
    {
        [Description("1位停止位")]
        UART_STOP_1 = 0,
        [Description("2位停止位")]
        UART_STOP_2,
    }
    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum uart_data_len
    {
        [Description("8位数据长度")]
        UART_DATA_LEN_8 = 0,
        [Description("9位数据长度")]
        UART_DATA_LEN_9
    }

    internal class UartModel: Communicate
    {
       
        private long _baud;
        public long baud { get { return _baud; } set { SetProperty(ref _baud, value); } }
        private uart_parity _parity;//0：无，1：奇，2：偶
        public uart_parity parity { get { return _parity; } set { SetProperty(ref _parity, value); } }
        private uart_stop_bits _stopBits;//0：1，1：1.5，2:2
        public uart_stop_bits stopBits { get { return _stopBits; } set { SetProperty(ref _stopBits, value); } }
        private uart_data_len _dataLen;//5,6,7,8
        public uart_data_len dataLen { get { return _dataLen; } set { SetProperty(ref _dataLen, value); } }
        public UartModel() : base(CommCmd.COMM_UART) { baud = 9600; parity = uart_parity.UART_PARITY_NONE; stopBits = uart_stop_bits.UART_STOP_1; dataLen = uart_data_len.UART_DATA_LEN_8; }
        public override string ToString()
        {
            switch (subCmd)
            {
                case 1:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("改变UART参数->");
                    sb.Append(baud.ToString()+",");
                    sb.Append(EnumExtensions.GetDescription(parity)+",");
                    sb.Append(EnumExtensions.GetDescription(stopBits) + ",");
                    sb.Append(EnumExtensions.GetDescription(dataLen) + ",");
                    sb.Append("\n");
                    return sb.ToString();
                case 2:
                    return "UART写入数据->" + sendBytes + "\n";
                case 3:
                    return "UART等待读取到" + receiveBytes + "\n";
                default:
                    return "未知命令" + "\n";
            }
        }
    }
}
